package com.binarysoft.cmdhandler.common.activiti;

import java.util.List;
import java.util.StringTokenizer;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.repository.ProcessDefinition;

import com.binarysoft.service.common.activiti.ActivitiService;
import common.framework.command.handler.AbstractCommandHandler;
import common.framework.command.helper.CommandHelper;
import common.framework.command.server.Terminal;
import common.framework.dsb.annotation.DSBCmd;
import common.framework.dsb.client.DefaultServiceFactory;
import common.framework.dsb.client.ServiceFactory;

@DSBCmd("act")
public class ActivitiCmdHandler extends AbstractCommandHandler {

	@Override
	public void handle(String args, Terminal terminal) throws Exception {

		terminal.println("");
		StringTokenizer stier = new StringTokenizer(args);
		int count = stier.countTokens();
		if (count < 2) {
			terminal.println(" Usage:");
			terminal.println(" act list                                   -- list all process definitions.");
			terminal.println("");
			return;
		}
		stier.nextToken();
		String cmd = stier.nextToken();
		if ("list".equalsIgnoreCase(cmd) && count == 2) {

			ServiceFactory serviceFactory = new DefaultServiceFactory();
			ActivitiService activitiService = serviceFactory.lookup("DSB/ActivitiService");
			ProcessEngine processEngine = activitiService.getProcessEngine();

			List<ProcessDefinition> list = processEngine.getRepositoryService()// 与流程定义和部署对象相关的Service
					.createProcessDefinitionQuery()// 创建一个流程定义查询
					/* 指定查询条件,where条件 */
					// .deploymentId(deploymentId)//使用部署对象ID查询
					// .processDefinitionId(processDefinitionId)//使用流程定义ID查询
					// .processDefinitionKey(processDefinitionKey)//使用流程定义的KEY查询
					// .processDefinitionNameLike(processDefinitionNameLike)//使用流程定义的名称模糊查询

					/* 排序 */
					.orderByProcessDefinitionVersion().asc()// 按照版本的升序排列
					// .orderByProcessDefinitionName().desc()//按照流程定义的名称降序排列

					.list();// 返回一个集合列表，封装流程定义
			// .singleResult();//返回唯一结果集
			// .count();//返回结果集数量
			// .listPage(firstResult, maxResults)//分页查询

			if (list != null && list.size() > 0) {
				for (ProcessDefinition processDefinition : list) {
					System.out.println("流程定义ID:" + processDefinition.getId());// 流程定义的key+版本+随机生成数
					System.out.println("流程定义名称:" + processDefinition.getName());// 对应HelloWorld.bpmn文件中的name属性值
					System.out.println("流程定义的key:" + processDefinition.getKey());// 对应HelloWorld.bpmn文件中的id属性值
					System.out.println("流程定义的版本:" + processDefinition.getVersion());// 当流程定义的key值相同的情况下，版本升级，默认从1开始
					System.out.println("资源名称bpmn文件:" + processDefinition.getResourceName());
					System.out.println("资源名称png文件:" + processDefinition.getDiagramResourceName());
					System.out.println("部署对象ID:" + processDefinition.getDeploymentId());
					System.out.println("################################");
				}
			}

			String[] tableHAD = { "ProcDef ID", "ProcDef Name", "ProcDef Key", "ProcDef Ver", "bpmn file", "png file", "deploy ID" };
			String[] tableGAP = { "----------", "------------", "-----------", "-----------", "---------", "--------", "---------" };
			int[] format = { 28, 28, 22, 18, 40, 40, 40 };
			terminal.println(CommandHelper.format(tableHAD, format));
			terminal.println(CommandHelper.format(tableGAP, format));

			for (ProcessDefinition processDefinition : list) {
				Object[] row = { processDefinition.getId(), processDefinition.getName(), processDefinition.getKey(), processDefinition.getVersion(), processDefinition.getResourceName(),
						processDefinition.getDiagramResourceName(), processDefinition.getDeploymentId() };
				terminal.println(CommandHelper.format(row, format));
			}
			terminal.println("");

		} else if ("get".equalsIgnoreCase(cmd) && count == 4) {
		}
	}

}
